<template>
  <div class="login-container">
    <!-- 左侧背景区域 -->
    <div class="login-left">
      <div class="left-content">
        <h1>欢迎登录</h1>
        <p>智慧商城管理后台</p>
      </div>
    </div>

    <!-- 右侧登录表单区域 -->
    <div class="login-right">
      <div class="form-wrapper">
        <!-- Logo -->
        <!-- <div class="logo">商户管理后台</div> -->

        <!-- 登录表单 -->
        <div class="form-box">
          <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="login-form">
            <el-form-item prop="username">
              <el-input 
                v-model="loginForm.username" 
                placeholder="账号" 
                class="input-field"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input 
                v-model="loginForm.password" 
                type="password" 
                placeholder="密码" 
                show-password
                class="input-field"
              />
            </el-form-item>

            <el-form-item>
              <el-button 
                type="primary" 
                @click="submitForm" 
                :loading="loading" 
                class="submit-btn"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <!-- 底部信息 -->
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import { useRouter } from 'vue-router'

const loginForm = reactive({
  username: '',
  password: ''
})

const loginFormRef = ref(null)
const loading = ref(false)
const router = useRouter()

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const submitForm = async () => {
  if (!loginFormRef.value) {
    ElMessage.error('表单初始化失败')
    return
  }
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const response = await axios.post('/api/login', loginForm)
        console.log(response)
        if (response.data.code === "200") {
          localStorage.setItem('token', response.data.data.token)
          localStorage.setItem('userInfo', JSON.stringify({
            userId: response.data.data.userId,
            username: response.data.data.username
          }))
          ElMessage.success('登录成功')
          router.push('/admin')
        } else {
          ElMessage.error(response.data.message || '登录失败，请检查用户名或密码')
        }
      } catch (error) {
        console.error("Login error:", error)
        ElMessage.error('登录失败，请稍后重试或联系管理员')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* 左侧区域 - 减小占比到35% */
.login-left {
  flex: 0 0 60%;
  background: linear-gradient(135deg, #4A90E2 0%, #357ABD 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 300"><path d="M0,100 Q100,80 200,100 T400,100 L400,120 Q300,110 200,120 T0,120 Z" fill="rgba(255,255,255,0.1)"/><path d="M0,150 Q100,130 200,150 T400,150 L400,170 Q300,160 200,170 T0,170 Z" fill="rgba(255,255,255,0.08)"/><circle cx="80" cy="60" r="2" fill="rgba(255,255,255,0.3)"/><circle cx="320" cy="80" r="1.5" fill="rgba(255,255,255,0.4)"/><circle cx="150" cy="200" r="1" fill="rgba(255,255,255,0.2)"/></svg>');
  opacity: 0.6;
}

.left-content {
  text-align: center;
  z-index: 2;
}

.left-content h1 {
  font-size: 36px;
  font-weight: 400;
  margin-bottom: 8px;
  letter-spacing: 1px;
}

.left-content p {
  font-size: 16px;
  font-weight: 300;
  opacity: 0.9;
}

/* 右侧区域 - 增大到65% */
.login-right {
  flex: 0 0 40%;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.form-wrapper {
  width: 100%;
  max-width: 320px;
  text-align: center;
}

/* Logo - 紧凑设计 */
.logo {
  width: 80px;
  height: 50px;
  background: #4A90E2;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  border-radius: 4px;
  margin: 0 auto 30px;
}

/* 表单容器 - 去掉多余装饰 */
.form-box {
  margin-bottom: 50px;
}

.login-form {
  text-align: left;
}

:deep(.el-form-item) {
  margin-bottom: 16px;
}

/* 输入框 - 简洁样式 */
:deep(.input-field .el-input__wrapper) {
  height: 40px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  background: #ffffff;
  box-shadow: none;
  padding: 0 12px;
}

:deep(.input-field .el-input__wrapper:hover) {
  border-color: #4A90E2;
}

:deep(.input-field.is-focus .el-input__wrapper) {
  border-color: #4A90E2;
  box-shadow: none;
}

:deep(.input-field .el-input__inner) {
  font-size: 14px;
  color: #333333;
  height: 38px;
  line-height: 38px;
}

:deep(.input-field .el-input__inner::placeholder) {
  color: #c0c4cc;
}

/* 登录按钮 - 简洁圆角 */
.submit-btn {
  width: 100%;
  height: 40px;
  background: #4A90E2;
  border: none;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 400;
  margin-top: 8px;
}

.submit-btn:hover {
  background: #357ABD;
}

/* 底部信息 - 紧凑布局 */
.footer-text {
  font-size: 11px;
  color: #999999;
  line-height: 1.4;
}

.footer-text p {
  margin-bottom: 6px;
}

.watermark {
  color: #cccccc;
  font-size: 10px;
}

/* 错误提示 */
:deep(.el-form-item__error) {
  font-size: 12px;
  color: #f56565;
  margin-top: 2px;
}

/* 响应式 */
@media (max-width: 1200px) {
  .login-left {
    flex: 0 0 40%;
  }
  .login-right {
    flex: 0 0 60%;
  }
}

@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }
  
  .login-left {
    flex: 0 0 30vh;
    min-height: 200px;
  }
  
  .left-content h1 {
    font-size: 28px;
  }
  
  .left-content p {
    font-size: 14px;
  }
  
  .login-right {
    flex: 1;
    padding: 30px 20px;
  }
  
  .form-wrapper {
    max-width: 280px;
  }
}

@media (max-width: 480px) {
  .login-left {
    flex: 0 0 25vh;
  }
  
  .login-right {
    padding: 20px 16px;
  }
  
  .logo {
    margin-bottom: 25px;
  }
  
  .form-box {
    margin-bottom: 40px;
  }
}
</style>